var helper = require('../../../library/helper.js')
import { App, epclass, navigate } from '../../../library/api/index'

Page({
  data: {
    isAudio: true,
    hideVideoArea: true,
    videoCtx: null,
    audioCtx: null,
    audioDuration: 0,
    audioCurrentDuration: 0,
    audioCurrentPercent: 0,
    audioCurrentDurationText: '0:00',
    isPlaying: false,
    playBtn: 'https://r.epclass.meadjohnson.joypaw.com/p/v2/icon_15.png',
    playBtns: {
      play: 'https://r.epclass.meadjohnson.joypaw.com/p/v2/icon_15.png',
      pause: 'https://r.epclass.meadjohnson.joypaw.com/p/v2/icon_59.png'
    },
    class: {
      cid: 0
    },
    reviews: [],
    tip: 'blank',
    tipData: {},
    navIndex: 'on',
    navMe: 'off'
  },
  onLoad: function(e) {
    this.setData({
      class: {
        cid: e.cid
      }
    })
    if(e.from !== undefined && e.from == 'me') {
      this.setData({
        navIndex: 'off',
        navMe: 'on'
      })
    }
    App.jice.onLoad(e)
  },
  onReady: function() {
    var cid = this.data.class.cid
    if(cid === undefined) {
      this.showGoIndexTip('课程获取失败，请返回首页再试~')
      return
    }
    epclass.getClass({ cid: cid }, r => {
      if(r.code == 200) {
        if(r.data.live_type == 2) {
          this.data.isAudio = false
        }
        this.setData({
          class: r.data,
          isAudio: this.data.isAudio
        })
        if(this.data.isAudio) {
          this.data.audioCtx = wx.createAudioContext('myAudio')
          this.data.audioDuration = helper.strToSecond(this.data.class.audio_duration)
        } else {
          this.data.videoCtx = wx.createVideoContext('myVideo')
        }
        this.setData({
          videoCtx: this.data.videoCtx,
          audioCtx: this.data.audioCtx,
          audioDuration: this.data.audioDuration
        })
      }
    })
    epclass.getReviews({ cid: cid }, r => {
      if(r.code == 200) {
        for(var i = 0; i < r.data.length; i++) {
          r.data[i]['v_c'] = 'audio_img'
        }
        this.setData({
          reviews: r.data
        })
      }
    })
    App.jice.track('page', {
      pagepath: '/pages/epclass/review/index',
      pageTitle: '回顾',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '课程回顾',
      class_id: cid
    })
  },
  onShow: function() {
    App.jice.track('appshow')
  },
  playVideo: function() {
    this.showVideoArea()
    this.pausePlayReplay()
    this.data.videoCtx.play()
    this.setData({
      isPlaying: true
    })
  },
  playAudio: function() {
    this.pausePlayReplay()
    if(this.data.isPlaying) {
      this.data.audioCtx.pause()
      this.setData({
        isPlaying: false,
        playBtn: this.data.playBtns.play
      })
    } else {
      this.data.audioCtx.play()
      this.setData({
        isPlaying: true,
        playBtn: this.data.playBtns.pause
      })
    }
  },
  audioPlayEnd: function() {
    this.setData({
      isPlaying: false,
      playBtn: this.data.playBtns.play
    })
  },
  audioPlaying: function(e) {
    this.setData({
      audioCurrentDuration: e.detail.currentTime,
      audioCurrentPercent: (e.detail.currentTime / this.data.audioDuration) * 100,
      audioCurrentDurationText: helper.secondToStr(e.detail.currentTime)
    })
  },
  changeAudioDuration: function(e) {
    this.data.audioCtx.pause()
    this.data.audioCurrentDuration = (e.detail.value / 100) * this.data.audioDuration
    this.data.audioCtx.seek(this.data.audioCurrentDuration)
    setTimeout(() => {
      this.data.audioCtx.play()
    }, 500)

    this.setData({
      isPlaying: true,
      playBtn: this.data.playBtns.pause
    })
  },
  start: function() {
    if(!this.data.isAudio) {
      this.data.videoCtx.play()
    } else {
      this.data.audioCtx.play()
      this.setData({
        isPlaying: true,
        playBtn: this.data.playBtns.pause
      })
    }
  },
  stop: function() {
    if(!this.data.isAudio) {
      this.data.videoCtx.pause()
    } else {
      this.data.audioCtx.pause()
      this.setData({
        isPlaying: false,
        playBtn: this.data.playBtns.play
      })
    }
  },
  currentReplayAudioCtx: null,
  currentReplayAudioCtxPlaying: null,
  currentReplayAudioCtxId: null,
  playReplay: function(event) {
    var clickId = event.target.dataset.id
    var clickAudioId = 'v-' + clickId
    if(this.currentReplayAudioCtx != null) {
      if(clickId == this.currentReplayAudioCtxId) {
        if(this.currentReplayAudioCtxPlaying) {
          this.currentReplayAudioCtx.pause()
          this.currentReplayAudioCtxPlaying = false
          this.changeReplayBarStyleByPlayStatus(clickId, false)
        } else {
          this.stop()
          this.currentReplayAudioCtx.play()
          this.currentReplayAudioCtxPlaying = true
          this.changeReplayBarStyleByPlayStatus(clickId, true)
        }
      } else {
        // 播放对象改变,停止去掉当前currentReplay*变量赋值
        this.stop()
        this.currentReplayAudioCtx.pause()
        this.currentReplayAudioCtx = null
        this.changeReplayBarStyleByPlayStatus(this.currentReplayAudioCtxId, false)
        // 把当前播放对象指定到currentReplay*变量上
        this.currentReplayAudioCtx = wx.createAudioContext(clickAudioId)
        this.currentReplayAudioCtxId = clickId
        this.currentReplayAudioCtx.play()
        this.currentReplayAudioCtxPlaying = true
        this.changeReplayBarStyleByPlayStatus(clickId, true)
      }
    } else {
      this.stop()
      this.currentReplayAudioCtx = wx.createAudioContext(clickAudioId)
      this.currentReplayAudioCtxId = clickId
      this.currentReplayAudioCtx.play()
      this.currentReplayAudioCtxPlaying = true
      this.changeReplayBarStyleByPlayStatus(clickId, true)
    }
  },
  playReplayEnd: function(e) {
    this.changeReplayBarStyleByPlayStatus(this.currentReplayAudioCtxId, false)
    this.currentReplayAudioCtx = null
    this.currentReplayAudioCtxPlaying = null
    this.currentReplayAudioCtxId = null
  },
  changeReplayBarStyleByPlayStatus: function(id, isPlay) {
    for(var i = 0; i < this.data.reviews.length; i++) {
      if(this.data.reviews[i]['id'] == id) {
        if(isPlay) {
          this.data.reviews[i]['v_c'] = 'audio_img play'
        } else {
          this.data.reviews[i]['v_c'] = 'audio_img'
        }
        break
      }
    }
    this.setData({
      reviews: this.data.reviews
    })
  },
  pausePlayReplay: function() {
    // 停止这种播放的回复语音,主要用于播放回顾的音频和视频回调事件
    if(this.currentReplayAudioCtx != null && this.currentReplayAudioCtxPlaying) {
      this.currentReplayAudioCtx.pause()
      this.changeReplayBarStyleByPlayStatus(this.currentReplayAudioCtxId, false)
    }
  },
  hideVideoArea: function() {
    this.setData({
      hideVideoArea: true
    })
  },
  showVideoArea: function() {
    this.setData({
      hideVideoArea: false
    })
  },
  showExpert: function() {
    this.setData({
      tip: 'expert',
      tipData: {
        expert: this.data.class.expert
      }
    })
    this.hideVideoArea()
  },
  closeExpertTip: function() {
    this.setData({
      tip: 'blank',
      tipData: {}
    })
    if(this.data.isPlaying) {
      this.showVideoArea()
    }
  },
  showGoIndexTip: function(msg) {
    this.setData({
      tip: 'go_index_tip',
      tipData: msg
    })
  },
  goIndex() {
    navigate.redirect(`/pages/index/index`)
  },
  mall: function() {
    this.setData({
      tip: 'go_mall_tip'
    })
  },
  goMall: function() {
    navigate.to('/pages/mall/index')
  },
  closeTip: function() {
    this.setData({
      tip: 'blank'
    })
  },
  onShareAppMessage: function() {
    App.jice.track('onshare', {
      shareto: 'friend',
      title: `与专家对话，做A+妈妈！快来回顾【${this.data.class.name}】`,
      path: `/pages/epclass/review/index?cid=${this.data.class.id}`
    })
    return {
      title: `与专家对话，做A+妈妈！快来回顾【${this.data.class.name}】`,
      desc: `我正在看【${this.data.class.name}}】的精华回顾，马上点进来学做A+妈妈吧！`,
      path: `/pages/epclass/review/index?cid=${this.data.class.id}`,
      success: () => {
        epclass.shareLogger({
          class_id: this.data.class.id,
          target: '课程回顾',
          action: 'appMessage'
        }, r => {
        })
      }
    }
  }
})